﻿using Awsc.DataAcquisitionSys.Core.Services.DataAcquisition;

namespace Awsc.DataAcquisitionSys.Api.BackgroundServices
{
    public class DataCollectBackgroundService : BackgroundService
    {
        private readonly IServiceScopeFactory serviceScopeFactory;
        private DataCollectEngine dataCollectEngine;


        public DataCollectBackgroundService(IServiceScopeFactory serviceScopeFactory)
        {
            this.serviceScopeFactory = serviceScopeFactory;
        }
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            using (var scope = serviceScopeFactory.CreateScope())
            {
                var serviceProvider = scope.ServiceProvider;
                dataCollectEngine = serviceProvider.GetRequiredService<DataCollectEngine>();
                await dataCollectEngine.StartAsync();

                //using var timer = new PeriodicTimer(TimeSpan.FromMilliseconds(200));
                //return Task.CompletedTask;
            }
        }
        public override async Task StopAsync(CancellationToken cancellationToken)
        {
            await dataCollectEngine?.StopAsync();
            await base.StopAsync(cancellationToken);
        }
         

    }
}
